package com.tmy.service.Impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tmy.Dto.OrderDto;
import com.tmy.Dto.PageInfo;
import com.tmy.Dto.Result;
import com.tmy.entity.Hotel;
import com.tmy.entity.Order;
import com.tmy.entity.RoomInfo;
import com.tmy.entity.RoomStatus;
import com.tmy.mapper.HotelMapper;
import com.tmy.mapper.OrderMapper;
import com.tmy.mapper.RoomInfoMapper;
import com.tmy.mapper.RoomStatusMapper;
import com.tmy.service.OrderService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private HotelMapper hotelMapper;

    @Resource
    private RoomInfoMapper roomInfoMapper;

    @Resource
    private RoomStatusMapper roomStatusMapper;


    /**
     * 新增订单
     */
    @Override
    public Result addOrder(Order order){
        Result result =new Result();
        try {
            order.setCreateTime(new Date());
            order.setOrderId(String.valueOf(System.currentTimeMillis()));
            int res = orderMapper.insert(order);
            RoomStatus roomStatus = new RoomStatus();
            roomStatus.setRoomId(Integer.valueOf(order.getRoomId()));
            roomStatus.setStartDate(order.getStartDate());
            roomStatus.setEndDate(order.getStartDate());
            if(res==1){
                roomStatusMapper.insert(roomStatus);
                result.setCode(200);
                result.setMsg("预定成功");
            }else {
                result.setCode(400);
                result.setMsg("预定失败！");
            }

        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }
    /**
     * 删除订单
     */
    @Override
    public Result deleteOrder(Order order){
        Result result =new Result();
        try {
            orderMapper.deleteById(order);
            result.setCode(200);
            result.setMsg("删除成功");
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }
    /**
     * 修改订单
     */
    @Override
    public Result updateOrder(Order order){
        Result result =new Result();
        try {
            orderMapper.updateById(order);
            if (Objects.equals(order.getStatus(), "4")||Objects.equals(order.getStatus(), "5")){
                Order order1 = orderMapper.selectById(order);
                LambdaQueryWrapper<RoomStatus> lqw =new LambdaQueryWrapper<>();
                lqw.eq(RoomStatus::getRoomId,Integer.valueOf(order1.getRoomId()));
                lqw.eq(RoomStatus::getStartDate,order1.getStartDate());
                lqw.eq(RoomStatus::getEndDate,order1.getEndDate());
                roomStatusMapper.delete(lqw);
            }
            result.setCode(200);
            result.setMsg("更新成功");
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }
    /**
     * 查询订单（用户查询自己订单）
     */
    @Override
    public Result queryOrder(Order order){
        Result result =new Result();
        try {
            LambdaQueryWrapper<Order> lqw =new LambdaQueryWrapper<>();
            lqw.eq(Order::getUserId,order.getUserId());
            List<Order> orderList =orderMapper.selectList(lqw);
            List<OrderDto> orderDtoList =new ArrayList<>();
            for (Order item:orderList){
                OrderDto orderDto = JSON.parseObject(JSON.toJSONString(item),OrderDto.class);
                LambdaQueryWrapper<RoomInfo> lqwRoom =new LambdaQueryWrapper<>();
                lqwRoom.eq(RoomInfo::getId,item.getRoomId());
                List<RoomInfo> roomInfoList = roomInfoMapper.selectList(lqwRoom);
                orderDto.setRoomList(JSONArray.of(roomInfoList));
                LambdaQueryWrapper<Hotel> lqwHotel =new LambdaQueryWrapper<>();
                lqwHotel.eq(Hotel::getId,item.getHotelId());
                Hotel hotel =hotelMapper.selectOne(lqwHotel);
                orderDto.setHotelInfo(JSONObject.from(hotel));
                orderDtoList.add(orderDto);
            }
            result.setData(orderDtoList);
            result.setCode(200);
            result.setMsg("查询成功");
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }
    /**
     * 查询订单（分页查询）
     */
    @Override
    public Result queryOrderPage(PageInfo pageInfo){
        Result result =new Result();
        try {
            Page<Order> page = new Page<>(pageInfo.getPage(),pageInfo.getLimit());
            Page<Order> orderPage = orderMapper.selectPage(page, null);
            List<OrderDto> orderDtoList =new ArrayList<>();
            for (Order item: orderPage.getRecords()){
                OrderDto orderDto = JSON.parseObject(JSON.toJSONString(item),OrderDto.class);
                LambdaQueryWrapper<RoomInfo> lqwRoom =new LambdaQueryWrapper<>();
                lqwRoom.eq(RoomInfo::getId,item.getRoomId());
                List<RoomInfo> roomInfoList = roomInfoMapper.selectList(lqwRoom);
                orderDto.setRoomList(JSONArray.of(roomInfoList));
                LambdaQueryWrapper<Hotel> lqwHotel =new LambdaQueryWrapper<>();
                lqwHotel.eq(Hotel::getId,item.getHotelId());
                Hotel hotel =hotelMapper.selectOne(lqwHotel);
                orderDto.setHotelInfo(JSONObject.from(hotel));
                orderDtoList.add(orderDto);
            }
            orderPage.setRecords(null);
            Page orderDtoPage =JSONObject.parseObject(JSON.toJSONString(orderPage),Page.class);
            orderDtoPage.setRecords(orderDtoList);
            result.setData(orderDtoPage);
            result.setCode(200);
            result.setMsg("订单查询成功");
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }
}
